const jwt = require("jsonwebtoken");
const { PRIVATE_KEY, PUBLIC_KEY } = require("../config/screct");

class LoginController {
  sign(ctx, next) {
    // 1.获取用户信息
    const { id, name } = ctx.user;
    // console.log(ctx.user, "ctx.user");
    try {
      // 2.颁发令牌token
      const token = jwt.sign({ id, name }, PRIVATE_KEY, {
        expiresIn: 24 * 60 * 60,
        algorithm: "RS256",
      });

      // 3.返回用户信息
      ctx.body = { code: 200, data: { id, name, token } };
    } catch (error) {
      console.log(error, "error");
    }
  }
  test(ctx, next) {
    ctx.body = "身份验证通过";
  }
}
module.exports = new LoginController();
